package vashion.azeroth.api.controller.member;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.api.controller.base.BaseController;
import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.contant.PersonalMyInfoErrorEnum;
import vashion.azeroth.core.personal.pojo.PersonalHisInfo;
import vashion.azeroth.core.personal.pojo.PersonalMember;
import vashion.azeroth.core.personal.pojo.PersonalMyInfo;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalMyInfoService;

@Controller
@RequestMapping("/member")
public class MyInfoController<T> extends BaseController {

	private final static Logger log = LoggerFactory
			.getLogger(AddressController.class);

	@Autowired
	private PersonalMyInfoService<T> personalMyInfoService;
	

	@RequestMapping("getHisInfo.htm")
	@ResponseBody
	public ApiResult<PersonalHisInfo> getHisInfo(
			@ModelAttribute PersonalHisInfo memberInfo) {
//		@ModelAttribute PersonalMember member) {
		
		ApiResult<PersonalHisInfo> result = new ApiResult<PersonalHisInfo>();
		try {
			if (null == memberInfo) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == memberInfo.getHisMemberId()
					|| StringUtils.isBlank(String.valueOf(memberInfo
							.getHisMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "他的会员ID"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}
			
			if (null == memberInfo.getHisMemberId()
					|| StringUtils.isBlank(String.valueOf(memberInfo
							.getHisMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "我的会员ID"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			result =  personalMyInfoService.getHisInfo(memberInfo);
		} catch (Exception e) {
			log.error("获取会员个人信息异常", e);
			return ApiResult.newErrorResult(
					PersonalMyInfoErrorEnum.GET_MEM_INFO_FAIL.getErrorCode(),
					PersonalMyInfoErrorEnum.GET_MEM_INFO_FAIL.getErrorMessage());
		}
		return result;
	}
	
	
	@RequestMapping("getMyInfo.htm")
	@ResponseBody
	public ApiResult<PersonalMyInfo> getMyInfo(
			@ModelAttribute PersonalMember member) {

		ApiResult<PersonalMyInfo> result = new ApiResult<PersonalMyInfo>();
		try {
			if (null == member) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "参数"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			if (null == member.getMemberId()
					|| StringUtils.isBlank(String.valueOf(member
							.getMemberId()))) {
				return ApiResult.newErrorResult(
						GenericErrorEnum.NOT_EMPTY.getErrorCode(), "会员ID"
								+ GenericErrorEnum.NOT_EMPTY.getErrorMessage());
			}

			result = personalMyInfoService.getMyInfo(member);
		} catch (Exception e) {
			log.error("获取会员个人信息异常", e);
			return ApiResult.newErrorResult(
					PersonalMyInfoErrorEnum.GET_MEM_INFO_FAIL.getErrorCode(),
					PersonalMyInfoErrorEnum.GET_MEM_INFO_FAIL.getErrorMessage());
		}
		return result;
	}


}
